package me.payge.swipecardview;

import com.lorentzos.flingswipe.ScreenUtils;
import com.lorentzos.flingswipe.SwipeFlingAdapterView;
import com.payge.swipecardview.ResourceTable;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;

import java.security.SecureRandom;
import java.util.ArrayList;

public class MainAbility extends Ability implements Component.ClickedListener, SwipeFlingAdapterView.onFlingListener, SwipeFlingAdapterView.OnItemClickListener {
    int [] headerIcons = {
            ResourceTable.Media_i1,
            ResourceTable.Media_i2,
            ResourceTable.Media_i3,
            ResourceTable.Media_i4,
            ResourceTable.Media_i5,
            ResourceTable.Media_i6    };

    String[] names = {"张三","李四","王五","小明","小红","小花"};

    String[] citys = {"北京", "上海", "广州", "深圳"};

    String[] edus = {"大专", "本科", "硕士", "博士"};

    String[] years = {"1年", "2年", "3年", "4年", "5年"};

    private int cardWidth;
    private int cardHeight;

    private SwipeFlingAdapterView swipeView;
    private InnerAdapter adapter;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        initView();
        loadData();
    }

    private void initView() {
        cardWidth = ScreenUtils.getWidthInPx(this) - ScreenUtils.vp2px(this, 2 * 18);
        cardHeight = ScreenUtils.getHeightInPx(this) - ScreenUtils.vp2px(this, 338);
        swipeView = (SwipeFlingAdapterView) findComponentById(ResourceTable.Id_swipe_view);
        if (swipeView != null) {
            swipeView.setIsNeedSwipe(true);
            swipeView.setFlingListener(this);
            swipeView.setOnItemClickListener(this);

            adapter = new InnerAdapter();
            swipeView.setItemProvider(adapter);
        }
        Component component = findComponentById(ResourceTable.Id_swipeLeft);
        if (component != null) {
            component.setClickedListener(this);
        }
        component = findComponentById(ResourceTable.Id_swipeRight);
        if (component != null) {
            component.setClickedListener(this);
        }
    }

    @Override
    public void onItemClicked(int itemPosition, Object dataObject) {
    }

    @Override
    public void removeFirstObjectInAdapter() {
        adapter.remove(0);
    }

    @Override
    public void onLeftCardExit(Object dataObject) {
    }

    @Override
    public void onRightCardExit(Object dataObject) {
    }

    @Override
    public void onAdapterAboutToEmpty(int itemsInAdapter) {
        if (itemsInAdapter == 3) {
            loadData();
        }
    }

    @Override
    public void onScroll(float scrollProgressPercent) {
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_swipeLeft:
                swipeView.swipeLeft();
                //swipeView.swipeLeft(250);
                break;
            case ResourceTable.Id_swipeRight:
                swipeView.swipeRight();
                //swipeView.swipeRight(250);
        }
    }

    private void loadData() {
        SecureRandom ran = new SecureRandom();;
        ArrayList<Talent> list = new ArrayList<>(10);
        Talent talent;
        for (int i = 0; i < 10; i++) {
            talent = new Talent();
            talent.headerIcon = headerIcons[i % headerIcons.length];
            talent.nickname = names[ran.nextInt(names.length-1)];
            talent.cityName = citys[ran.nextInt(citys.length-1)];
            talent.educationName = edus[ran.nextInt(edus.length-1)];
            talent.workYearName = years[ran.nextInt(years.length-1)];
            list.add(talent);
        }
        adapter.addAll(list);
    }

    private class InnerAdapter extends BaseItemProvider {

        ArrayList<Talent> objs;

        public InnerAdapter() {
            objs = new ArrayList<>();
        }

        public void addAll(java.util.Collection<Talent> collection) {
            if (isEmpty()) {
                objs.addAll(collection);
                notifyDataChanged();
            } else {
                objs.addAll(collection);
            }
        }

        public void clear() {
            objs.clear();
            notifyDataChanged();
        }

        public boolean isEmpty() {
            return objs.isEmpty();
        }

        public void remove(int index) {
            if (index > -1 && index < objs.size()) {
                objs.remove(index);
                notifyDataChanged();
            }
        }

        @Override
        public int getCount() {
            return objs.size();
        }

        @Override
        public Talent getItem(int position) {
            if(objs==null ||objs.size()==0) return null;
            return objs.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public Component getComponent(int position, Component component, ComponentContainer parent) {
            ViewHolder holder;
            Talent talent = getItem(position);
            if (component == null) {

                component = LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_card_new_item, parent, false);
                holder  = new ViewHolder();
                component.setTag(holder);
                component.setWidth(cardWidth);
                holder.portraitView = (Image) component.findComponentById(ResourceTable.Id_portrait);
                //holder.portraitView.getLayoutParams().width = cardWidth;
                holder.portraitView.setHeight(cardHeight);
                holder.nameView = (Text) component.findComponentById(ResourceTable.Id_name);
                //parentView.getLayoutParams().width = cardWidth;
                //holder.jobView = (TextView) component.findViewById(R.id.job);
                //holder.companyView = (TextView) component.findViewById(R.id.company);
                holder.cityView = (Text) component.findComponentById(ResourceTable.Id_city);
                holder.eduView = (Text) component.findComponentById(ResourceTable.Id_education);
                holder.workView = (Text) component.findComponentById(ResourceTable.Id_work_year);
            } else {
                holder = (ViewHolder) component.getTag();
            }


            holder.portraitView.setPixelMap(talent.headerIcon);

            holder.nameView.setText(String.format("%s", talent.nickname));
            //holder.jobView.setText(talent.jobName);

            final String no = "暂无";

            holder.cityView.setHint(no);
            holder.cityView.setText(talent.cityName);
            ShapeElement cityElement = new ShapeElement();
            cityElement.setShape(ResourceTable.Media_home01_icon_location);
            holder.cityView.setAroundElements(null, cityElement, null , null);

            holder.eduView.setHint(no);
            holder.eduView.setText(talent.educationName);
            ShapeElement eduElement = new ShapeElement();
            eduElement.setShape(ResourceTable.Media_home01_icon_edu);
            holder.eduView.setAroundElements(null, eduElement, null , null);

            holder.workView.setHint(no);
            holder.workView.setText(talent.workYearName);
            ShapeElement workElement = new ShapeElement();
            workElement.setShape(ResourceTable.Media_home01_icon_work_year);
            holder.workView.setAroundElements(null, workElement, null , null);


            return component;
        }
    }

    private static class ViewHolder {
        Image portraitView;
        Text nameView;
        Text cityView;
        Text eduView;
        Text workView;
        //CheckedTextView collectView;
        Text collectView;
    }

    public static class Talent {
        public int headerIcon;
        public String nickname;
        public String cityName;
        public String educationName;
        public String workYearName;
    }
}
